﻿using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FileManagerGlobal_Library;
using FileManager_Client.PresentationLayer.Common;
using FileManager_Client.Utility;
using Microsoft.Win32;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class FileManager_Form
    {
         void FileManager_Form_Shown_SystemConfig(object sender, EventArgs e)
         {
             var systemConfig = UserLocalFile.ReadSystemConfig();
             if (null == systemConfig) return;
             Cb_AutoStartConfig.Checked = systemConfig.IsAutoStart;
             Cb_ShowMessage.Checked = systemConfig.IsShowMessage;
         }

         private void Btn_SaveConfig_Click(object sender, EventArgs e)
         {
             var systemConfig = new SystemConfig
                                    {
                                        IsAutoStart = Cb_AutoStartConfig.Checked,
                                        IsShowMessage = Cb_ShowMessage.Checked
                                    };
             UserLocalFile.SaveSystemConfig(systemConfig);
             var str1 = Process.GetCurrentProcess().MainModule.ModuleName;//可获得当前执行的exe的文件名。
             var str2 = Environment.CurrentDirectory;//获取和设置当前目录（即该进程从中启动的目录）的完全限定路径.
            // str2 += str1;
             if(Cb_AutoStartConfig.Checked)
             {
                 try
                 {
                     var runKey = Registry.LocalMachine.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryAutoStartKey, true);
                     if (null != runKey)
                     {
                         runKey.SetValue(GlobalVariable.ClientAppRegistryInfo.AutoStartNameCollection.RegistryAutoStartName, Application.ExecutablePath);
                         runKey.Close();
                     }
                 }
                 catch(Exception ex)
                 {
                     LogLoader.Instance.Log.Error("Btn_SaveConfig_Click has problem", ex);
                     XtraMessageBox.Show("保存设置出错，请联系管理员!",
                                         GlobalString.System_Exception_String.MbxErrorStr, MessageBoxButtons.OK,
                                         MessageBoxIcon.Error);
                 }
             }
             else
             {
                 var hkml = Registry.LocalMachine;
                 var software = hkml.OpenSubKey(GlobalVariable.ClientAppRegistryInfo.RegistryAutoStartKey, true);
                 if (null != software)
                     software.DeleteValue(GlobalVariable.ClientAppRegistryInfo.AutoStartNameCollection.RegistryAutoStartName);
             }
             XtraMessageBox.Show("保存设置成功!",
                                         GlobalString.System_Config_String.MbxInfoStr, MessageBoxButtons.OK,
                                         MessageBoxIcon.Information);
         }
    }
}